﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.WebInfo;
using com.subb.WebInfo;

namespace WebAdmin.Controllers
{
    [Authorize]
    public class JobsController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllJobs(StoreRequestParameters parms)
        {
            WI_JobsDAL jdal = new WI_JobsDAL();
            int ttlCount;
            List<WI_Jobs> joblist = jdal.GetAllJobs(parms.Start, parms.Limit, out ttlCount);
            return this.Store(joblist, ttlCount);
        }

        [HttpPost]
        public ActionResult DeleteJob(List<Jobs> delJobs)
        {
            try
            {
                WI_JobsDAL jdal = new WI_JobsDAL();
                foreach (var job in delJobs)
                {
                    jdal.RemoveJob(job.JobID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        [HttpPost]
        public ActionResult DeleteJobs(List<Jobs> delJobs)
        {
            try
            {
                WI_JobsDAL jdal = new WI_JobsDAL();
                foreach (var job in delJobs)
                {
                    jdal.RemoveJob(job.JobID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewJobWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建招聘信息",
                Width = 600,
                Height = 400,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutAdd,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateJob"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateJob()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateJob(Jobs vjob)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_JobsDAL jdal = new WI_JobsDAL();
                jdal.CreateJob(vjob);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "招聘信息创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditJobWindow(int jobId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑招聘信息",
                Width = 600,
                Height = 400,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutEdit,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditJob"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("jobId", jobId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditJob(int jobId)
        {
            WI_JobsDAL jdal = new WI_JobsDAL();
            WI_Jobs wijob = jdal.GetJob(jobId);
            Jobs job = Jobs.AsJobs(wijob);
            return View(job);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditJob(Jobs vjob)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_JobsDAL jdal = new WI_JobsDAL();
                jdal.UpdateJob(vjob.JobID, vjob);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "招聘信息更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
